package edu.ccut.saturn.manager.dict.core;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IDataDictTypeInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnDictionaryManager;

public class UpdateDictType extends DefaultComponent {

	private static final String DICT_UPDATE_SQL = "update saturn_dict_type set name=? where type = ?";

	@SuppressWarnings( { "unchecked", "deprecation" })
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String type = (String) parameterContext.getParameterValue(0);
		String name = (String) parameterContext.getParameterValue(1);
		
		String updateSucc = "false";

		PreparedStatement preparedStatement = null;		
		try {

			preparedStatement = parameterContext.getConnection().prepareStatement(DICT_UPDATE_SQL);

			preparedStatement.setString(1, name);
			preparedStatement.setString(2, type);
			
			int result = preparedStatement.executeUpdate();
			
			if(result == 1) {
			    updateSucc = "true";
			}

		} catch (SQLException e) {
			logger.error("found exception when execute Component Node "
					+ this.getComponentFullKey(), e);
		} finally {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
		}

		if (updateSucc == "true") {
			IDataDictTypeInfo dataDictTypeInfo = SaturnDictionaryManager
					.getInstance().getDataDictTypeInfo(type);
			dataDictTypeInfo.setName(name);
		}

		parameterContext.setParameter(2, updateSucc);
	}
}
